package com.modular.scene.opt;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.c.opt.ComOpt;
import com.modular.scene.bean.SceneWeatherDo;
import com.modular.scene.bo.SceneWeatherQueryBo;
import ntd.util.ToolLock;

import java.util.List;

public class OptSceneWeather extends ComOpt<SceneWeatherDo> {

    public OptSceneWeather() {
        super("things_scene_weather");
    }

    /**
     * 场景天气保存
     * @param sceneId
     * @param weather
     */
    public void weatherCurrDay(String sceneId, int weather) {
        ToolLock.lockRun("weatherCurrDay" + sceneId, () -> {
            DateTime beginOfDay = DateUtil.beginOfDay(DateUtil.date());

            SceneWeatherDo sceneWeatherDo = query().eq("scene_id", sceneId)
                    .eq("datetime", DateUtil.formatDateTime(beginOfDay)).getBean();

            if (ObjectUtil.isNull(sceneWeatherDo)) {
                sceneWeatherDo = new SceneWeatherDo();
                sceneWeatherDo.setWeather(weather);
                sceneWeatherDo.setDatetime(beginOfDay);
                sceneWeatherDo.setSceneId(sceneId);

                // 同步数据
                syncBean(sceneWeatherDo);
                return;
            }

            if (sceneWeatherDo.getWeather() < weather) {
                // 当前更新的天气级别大于存储的级别才执行天气更新
                sceneWeatherDo.setWeather(weather);
                // 同步数据
                syncBean(sceneWeatherDo);
            }
        });
    }

    public void weatherReportDay(SceneWeatherQueryBo sceneWeatherQueryBo) {
        List<SceneWeatherDo> sceneWeatherDos = query().ge("created_date", sceneWeatherQueryBo.getStartTime()).le("created_date", sceneWeatherQueryBo.getEndTime())
                .listBean();

        for (SceneWeatherDo sceneWeatherDo : sceneWeatherDos) {
//            sceneWeatherDo.
        }
    }
}
